$(document).ready(function () {
    query();
    query2();
    select();
    courseType();
});

function select() {
    $(document).on('click', '#selectMark', function () {
        //var $stuInfoId=5;
        var tbody = $("#txug");
        var termYear = $("#termYear option:selected").val();
        var termNum = $("#termNum option:selected").val();
        var courseType = $("#courseType option:selected").val();
        var courseId = $("#courseId").val();
        var courseName = $("#courseName").val();
        var url = "http://localhost:8080/mark/selectFailGrade?stuInfoId=" + 1 + "&termYear=" + termYear + "&termNum=" + termNum + "&courseType=" + courseType + "&courseId=" + courseId + "&courseName=" + courseName;
        var table = $('#dataTable').dataTable()
        if (table) {
            table.fnDestroy();
        }

        $('#dataTable').dataTable({

            "processing": true,
            "serverSide": true,
            "bInfo": false,//是否显示是否启用底边信息栏
            "sAjaxDataProp": "data.markList",
            "sAjaxSource": url,
            "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
            "fnPreDrawCallback": function (oSettings) {
                $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
                $('.dataTables_length select').addClass('form-control input-small');
            },
            "oLanguage": {
                "sSearch": "",
                "sLengthMenu": "<span>_MENU_ entries</span>",
                "sZeroRecords": "暂无相关信息",
            },
            "bJQueryUI": false,
            'bAutoWidth': false,
            "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
            "aoColumnDefs": [
                {
                    "mDataProp": null,
                    "sDefaultContent": "<a href='#'><span  class='label label-primary mr10 mb10' id='apply'>申请自修</span></a>",
                    "aTargets": [-1]
                }
            ],
            "aoColumns": [
                {"mDataProp": "markId"},
                {"mDataProp": "termYear"},
                {"mDataProp": "termNum"},
                {"mDataProp": "courseId"},
                {"mDataProp": "courseName"},
                {"mDataProp": "courseType"},
                {"mDataProp": "courseCredithour"},
                {"mDataProp": "mark"},
                {"mDataProp": ""}
            ],
        })

    });
};
//成绩查询页面--所有不及格成绩
var query = function () {
    $('#dataTable').dataTable({
        "bDeferRender": true,
        "sAjaxDataProp": "data.markList",
        "sAjaxSource": "http://localhost:8080/mark/selectFailGrade?stuInfoId=" + 1,
        "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
        "fnPreDrawCallback": function (oSettings) {
            $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
            $('.dataTables_length select').addClass('form-control input-small');
        },
        "oLanguage": {
            "sSearch": "",
            "sLengthMenu": "<span>_MENU_ entries</span>",
            "sZeroRecords": "暂无相关信息",
        },
        "bJQueryUI": false,
        "bAutoWidth": false,
        "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
        "aoColumnDefs": [
            {
                "mDataProp": null,
                "sDefaultContent": "<a href='#'><span  class='label label-primary mr10 mb10' id='apply'>申请自修</span></a>",
                "aTargets": [-1]
            }
        ],
        "aoColumns": [
            {"mDataProp": "markId"},
            {"mDataProp": "termYear"},
            {"mDataProp": "termNum"},
            {"mDataProp": "courseId"},
            {"mDataProp": "courseName"},
            {"mDataProp": "courseType"},
            {"mDataProp": "courseCredithour"},
            {"mDataProp": "mark"},
            {"mDataProp": ""}

        ],
    })
};

//自己的自修申请
var query2 = function () {
    $('#dataTable2').dataTable({
        "bDeferRender": true,
        "sAjaxDataProp": "data.list",
        "sAjaxSource": "http://localhost:8080/selfStudy/queryMySelf?stuInoId=" + 1,
        "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
        "fnPreDrawCallback": function (oSettings) {
            $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
            $('.dataTables_length select').addClass('form-control input-small');
        },
        "oLanguage": {
            "sSearch": "",
            "sLengthMenu": "<span>_MENU_ entries</span>",
            "sZeroRecords": "暂无相关信息",
        },
        "bJQueryUI": false,
        "bAutoWidth": false,
        "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",

        "aoColumns": [
            {"mDataProp": "studybyoneselfId"},
            {"mDataProp": "stuInfoId"},
            {"mDataProp": "courseId"},
            {"mDataProp": "courseName"},
            {"mDataProp": "studybyoneselfFlag",
                "mRender": function(data) {
                    var value = "";
                    if (data==0){
                        value = "<span  class='label label-danger mr10 mb10' id='agree'>拒绝</span>";
                    }
                    if (data==1){
                        value = "<span  class='label label-success mr10 mb10' id='agree'>同意</span>";
                    }
                    if (data==2){
                        value = "<span  class='label label-yellow mr10 mb10' id='agree'>审核中</span>";
                    }
                    return value;
                }
            }

        ],
    })
};



$(document).on('click', '#apply', function () {


    var course_id = $(this).parents("tr").children(0).eq(3).text();

    //封装成对象提交
    var data = {
        stuInfoId:1,
        courseId:course_id
    };

    $.ajax({
        url: "http://localhost:8080/selfStudy/add",
        type: "POST",
        async: false,
        data:JSON.stringify(data),
        dataType: "json",
        contentType:"application/json",
        success:function (data) {
            console.log(data)
            var msg="申请成功";
            if (data.code==0){
                $.each(data.data,function (key,value) {
                    if(key=="0604"){
                        msg=value;
                        layer.msg(msg);
                        setTimeout('delayer()',3000)
                    }
                    if(key=="0603"){
                        msg=value;
                        layer.msg(msg);
                        setTimeout('delayer()',3000)
                    }

                });
            } else {
                layer.msg(msg);
                location.reload();
            }


        },
        error:function () {
            layer.alert("你的网络太慢了！添加失败!再试一次吧！");
        }
    });


});


//下拉列表获取课程类别
function courseType() {
    var cType = $("#courseType");
    $.ajax({
        url: "http://localhost:8080/mark/selectCourseType",
        type: "GET",
        async: false,
        dataType: "json",
        success: function (data) {
            var str = "";
            d = data.data;
            ctype = d[1]
            for (x in ctype) {
                str += "<option value='" + ctype[x].courseType + "'>" + ctype[x].courseType + "</option>"
            }
            cType.append(str);
        },
        error: function () {
            layer.alert("暂无记录")
        }
    })
}
